/*
 * All rights Reserved, Designed By DataDriver
 * Copyright:    DataDriver.Inc
 * Company:      Zhuo Wo Infomation Technology (ShangHai) CO.LTD
 */
package com.sun.pc;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 指定指定线程进行操作
 * 1A,2B,3C
 */
public class F {

    public static void main(String[] args) {
        Data data = new Data();
        new Thread((
        ) -> {
            for (int i = 0; i < 10; i++) {
                data.printlnA();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                data.printlnB();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                data.printlnC();
            }
        }, "C").start();
        System.out.println();
    }
}


class Data {

    private int num = 1;

    Lock lock = new ReentrantLock();
    Condition conditionA = lock.newCondition();
    Condition conditionB = lock.newCondition();
    Condition conditionC = lock.newCondition();




    //执行的顺序，业务，判断，等待，唤醒

    public void printlnA() {
        lock.lock();
        try {
            while (num != 1) {
                //等待
                conditionA.await();
            }
            System.out.println("AAAAA");
            //唤醒B
            num = 2;
            conditionB.signal();
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printlnB() {
        lock.lock();
        try {
            while (num != 2) {
                //等待
                conditionB.await();
            }
            System.out.println("BBBB");
            num = 3;
            //唤醒C
            conditionC.signal();
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printlnC() {
        lock.lock();
        try {
            while (num != 3) {
                //等待
                conditionC.await();
            }
            System.out.println("CCCC");
            num = 1;
            //唤醒A
            conditionA.signal();
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


}
